package controller

import (
	"context"
	pb "gitee.com/micro-services/protocol/verify/server"
	log "github.com/sirupsen/logrus"
	"micro.verify.server/ao"
	"micro.verify.server/plugins"
)

type CaptchaController struct {}

func (ct *CaptchaController) GenerateCaptcha(ctx context.Context, r *pb.GenerateCaptchaReq, w *pb.GenerateCaptchaRsp) error {
	log.Infof("call request:%+v", r)

	captchaID, captchaCode, err := ao.GenerateCaptcha(ao.CaptchaParams{
		CaptchaType: r.CaptchaType,
		Height:      r.Height,
		Width:       r.Width,
	})
	if err != nil {
		log.Errorf("generate captcha error:%+v", err)
		return err
	}

	w.CaptchaID = captchaID
	w.CaptchaCode = captchaCode

	plugins.DealWithTracer(ctx, "go.micro.verify.server.GenerateCaptcha", r, w)
	return nil
}

func (ct *CaptchaController) VerifyCaptcha(ctx context.Context, r *pb.VerifyCaptchaReq, w *pb.VerifyCaptchaRsp) error {
	log.Infof("call request:%+v", r)

	b := ao.VerifyCaptcha(r.CaptchaID, r.CaptchaCode)

	w.Result = b
	plugins.DealWithTracer(ctx, "go.micro.verify.server.VerifyCaptcha", r, w)
	return nil
}
